package com.guoyh.ext

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.guoyh.bilbase.ext.util.decoration.DefaultDecoration


/**
 * 绑定竖向Recyclerview
 */
fun RecyclerView.initVertical(
    bindAdapter: RecyclerView.Adapter<*>,
    itemDividerHeight: Int = 10,
    isScroll: Boolean = true
): RecyclerView {

    //Rlv 滑动到头的时候是否 有提示
    overScrollMode = View.OVER_SCROLL_NEVER

    this.vertical().divider {
        setDivider(itemDividerHeight, true)
        startVisible = false
        endVisible = false
    }

    adapter = bindAdapter
    isNestedScrollingEnabled = isScroll
    return this
}


/**
 * 纵向recyclerview
 * @receiver RecyclerView
 * @param baseAdapter BaseQuickAdapter<*, *>
 * @return RecyclerView
 */
fun RecyclerView.vertical(): RecyclerView {
    layoutManager = LinearLayoutManager(this.context)
    setHasFixedSize(true)
    return this
}

/**
 * 横向 recyclerview
 * @receiver RecyclerView
 * @return RecyclerView
 */
fun RecyclerView.horizontal(): RecyclerView {
    layoutManager = LinearLayoutManager(this.context).apply {
        orientation = RecyclerView.HORIZONTAL
    }
    setHasFixedSize(true)
    return this
}

/**
 * grid recyclerview
 * @receiver RecyclerView
 * @return RecyclerView
 */
fun RecyclerView.grid(count: Int): RecyclerView {
    layoutManager = GridLayoutManager(this.context, count)
    setHasFixedSize(true)
    return this
}

/**
 * 配置万能分割线
 * @receiver RecyclerView
 * @param block [@kotlin.ExtensionFunctionType] Function1<DefaultDecoration, Unit>
 * @return RecyclerView
 */
fun RecyclerView.divider(block: DefaultDecoration.() -> Unit): RecyclerView {
    val itemDecoration = DefaultDecoration(context).apply(block)
    addItemDecoration(itemDecoration)
    return this
}

